home *** CD-ROM | disk | FTP | other *** search
/ World of Education / World of Education.iso / world_x / xcoral16.zip / MAIN_EVE.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  11KB  |  503 lines

  1. /*
  2. ** Copyright 1989, 1992 by Lionel Fournigault
  3. **
  4. ** Permission to use, copy, and distribute for non-commercial purposes,
  5. ** is hereby granted without fee, providing that the above copyright
  6. ** notice appear in all copies and that both the copyright notice and this
  7. ** permission notice appear in supporting documentation.
  8. ** The software may be modified for your own purposes, but modified versions
  9. ** may not be distributed.
  10. ** This software is provided "as is" without any expressed or implied warranty.
  11. **
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Xlib.h>
  17. #include <X11/cursorfont.h>
  18. #include <X11/Xutil.h>
  19. #include <X11/keysym.h>
  20.  
  21. #include "xcoral.h"
  22. #include "browser.h"
  23. #include "flist.h"
  24.  
  25. EdWin            *CreateWindow ();
  26. Window            HandleMenu ();
  27. ST            *automate ();
  28. extern Browser        br;
  29.  
  30. static XEvent           event;
  31. static XEvent           *ev = &event;
  32.  
  33. static void HandleButtonPress (), HandleConfigure (), HandleEnter (),
  34.     HandleLeave (),    HandleVisibility (), HandleKeyRelease (),
  35.     HandleKeyPress (), HandleExpose (), HandleMap ();
  36.  
  37. extern void exit ();
  38.  
  39. #define MAX_EVENT 64
  40. void (* events_Handler[MAX_EVENT]) ();
  41. static void IgnoreEvent () {}
  42.  
  43. /*
  44. **    Function name : InitEvent
  45. **
  46. **    Description : Initialise la table des evennements.
  47. **    Input : 
  48. **    Ouput :
  49. */
  50. void InitEvent ()
  51. {
  52.     register int i;
  53.     for ( i= 0; i < MAX_EVENT; i++ )
  54.         events_Handler[i] = IgnoreEvent;
  55.  
  56.     events_Handler [ButtonPress] = HandleButtonPress;
  57.     events_Handler [ConfigureNotify] = HandleConfigure;
  58.     events_Handler [EnterNotify] = HandleEnter;
  59.     events_Handler [LeaveNotify] = HandleLeave;
  60.     events_Handler [VisibilityNotify] = HandleVisibility;
  61.     events_Handler [KeyRelease] = HandleKeyRelease;
  62.     events_Handler [KeyPress] = HandleKeyPress;
  63.     events_Handler [Expose] = HandleExpose;
  64.     events_Handler [MappingNotify] = HandleMap;
  65. }
  66.     
  67. /*
  68. **    Function name : WaitForEvent
  69. **
  70. **    Description : 
  71. **    Input : 
  72. **    Ouput :
  73. */
  74. void WaitForEvent ()
  75. {
  76.         ev = &event;
  77.  
  78.     for (;;) {
  79.         XNextEvent ( dpy, &event );
  80.         if ( event.type < MAX_EVENT )
  81.             (* events_Handler [event.type] ) ();
  82.     }
  83. }
  84.  
  85.  
  86. /*
  87. **    Function name : HandleMap
  88. **
  89. **    Description : 
  90. **    Input : 
  91. **    Ouput :
  92. */
  93. static void HandleMap ()
  94. {
  95. #ifdef DEBUG            
  96.     (void) fprintf ( stderr, "Keyboard Mapping\n" );
  97. #endif DEBUG
  98.     XRefreshKeyboardMapping ( (XMappingEvent *) ev );
  99. }
  100.  
  101.  
  102. /*
  103. **    Function name : HandleConfigure
  104. **
  105. **    Description : 
  106. **    Input : 
  107. **    Ouput :
  108. */
  109. static void HandleConfigure ()
  110. {
  111.     register EdWin    **t;
  112.  
  113. #ifdef DEBUG
  114.     ( void ) fprintf ( stderr, "Configure %d\n", ev -> xconfigure.window );
  115. #endif
  116.  
  117.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  118.         if ( *t == NULL ) continue;
  119.         if ( ev -> xconfigure.window == ( * t) -> w_frame ) {
  120.  
  121.                 if ( ((*t) -> width != ev -> xconfigure.width) ||
  122.                  ((*t) -> height != ev -> xconfigure.height) )
  123.  
  124.                 ConfigWindow ( *t, ev -> xconfigure.width,
  125.                       ev -> xconfigure.height );
  126. #ifdef DEBUG
  127.             ( void ) fprintf ( stderr,"width = %d height = %d\n",
  128.                 ev -> xconfigure.width, ev -> xconfigure.height  );
  129. #endif
  130.             while ( XCheckWindowEvent ( dpy,
  131.                 ev -> xconfigure.window,
  132.                 StructureNotifyMask, ev ));
  133.  
  134.             return;
  135.         }
  136.     }
  137.     if ( ev -> xconfigure.window == GetBrowserFrame () ) {
  138.         ConfigBrowser ( ev -> xconfigure.width,
  139.             ev -> xconfigure.height );
  140.         return;
  141.     }
  142.     if ( ev -> xconfigure.window == GetBrowserVisit () ) {
  143.         ConfigVisitWindow ( ev -> xconfigure.width,
  144.             ev -> xconfigure.height );
  145.         return;
  146.     }
  147. }
  148.  
  149.  
  150. /*
  151. **    Function name : HandleEnter
  152. **
  153. **    Description : 
  154. **    Input : 
  155. **    Ouput :
  156. */
  157. static void HandleEnter ()
  158. {
  159.     register EdWin    **t;
  160.  
  161. #ifdef DEBUG
  162.     ( void ) fprintf ( stderr, "Enter Notify %d mode = %d\n", 
  163.             ev -> xcrossing.window,
  164.             ev -> xcrossing.mode );
  165. #endif DEBUG
  166.  
  167.     if ( ev -> xcrossing.mode != NotifyNormal
  168.         &&  ev -> xcrossing.mode != NotifyUngrab )
  169.         return;
  170.  
  171.     if ( MouseInMenuBar ( dpy, ev -> xcrossing.window ) == True )
  172.         return;
  173.  
  174.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  175.         if ( *t == NULL ) continue; 
  176.         if ( (*t) -> w_frame != ev -> xcrossing.window ) continue;
  177. #ifdef DEBUG
  178.     ( void ) fprintf ( stderr, "Changement de contexte \n" );
  179. #endif
  180.         if ( XFindContext ( dpy, ev -> xcrossing.window,
  181.             EdContext, (caddr_t *) &edwin ) == XCNOENT ) {
  182.             (void) fprintf ( stderr,"Context Error\n" );
  183.             (void) exit(1);
  184.         }
  185.         MouseIn ( edwin -> text );
  186.         UnFreeseTextCursor ( edwin -> text );
  187.         TextCursorOn ( edwin -> text );
  188.         ChangeDir ( edwin -> text );
  189.         }
  190.     SetButton ( edwin -> mwin );
  191. }
  192.  
  193.  
  194. /*
  195. **    Function name : HandleLeave
  196. **
  197. **    Description : 
  198. **    Input : 
  199. **    Ouput :
  200. */
  201. static void HandleLeave ()
  202. {
  203.        register EdWin    **t;
  204.  
  205. #ifdef DEBUG
  206.        (void) fprintf ( stderr, "Leave Notify w = %d mode = %d detail = %d\n",
  207.                ev -> xcrossing.window, ev -> xcrossing.mode,
  208.                ev -> xcrossing.detail  );
  209. #endif DEBUG
  210.  
  211.        if ( ev -> xcrossing.mode != NotifyNormal )
  212.           return;
  213.  
  214.        if ( ev -> xcrossing.detail == NotifyInferior )
  215.           return;
  216.  
  217.        for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  218.                if ( *t == NULL ) continue;
  219.            if ( ev -> xcrossing.window == ( * t) -> w_frame ) {
  220.                 MouseOut( ( * t ) -> text );
  221.             TextCursorOff (( * t ) -> text );
  222.             FreeseTextCursor (( * t ) -> text );
  223.             DisplayMessage ( (*t)  -> mwin, " "  );
  224.             return;
  225.            }
  226.        }
  227. }
  228.  
  229.  
  230. /*
  231. **    Function name : HandleVisibility
  232. **
  233. **    Description : 
  234. **    Input : 
  235. **    Ouput :
  236. */
  237. static void HandleVisibility ()
  238. {
  239.     register EdWin    **t;
  240.  
  241.     if ( SetBrowserVisibility ( ev ) == True )
  242.         return;
  243.     else {
  244. #ifdef DEBUG
  245.     ( void ) fprintf ( stderr, "Visibility window = %d state = %d\n",
  246.         ev -> xvisibility.window, ev -> xvisibility.state );
  247. #endif
  248.         for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  249.             if ( *t == NULL ) continue;
  250.             if ( SetTextVisibility ( ev -> xvisibility.window,
  251.                 ( *t ) -> text, ev -> xvisibility.state ))
  252.                 break; 
  253.         }
  254.     }
  255. }
  256.  
  257.  
  258. /*
  259. **    Function name : HandleKeyRelease
  260. **
  261. **    Description : 
  262. **    Input : 
  263. **    Ouput :
  264. */
  265. static void HandleKeyRelease ()
  266. {
  267.     register EdWin    **t;
  268.     
  269.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  270.         if ( *t == NULL ) continue;
  271. #ifdef DEBUG
  272.         ( void ) fprintf ( stderr, "Key Release\n" );
  273. #endif
  274.     }            
  275. }
  276.  
  277.  
  278. /*
  279. **    Function name : HandleKeyPress
  280. **
  281. **    Description : 
  282. **    Input : 
  283. **    Ouput :
  284. */
  285. static void HandleKeyPress ()
  286. {
  287.     ST *stat;
  288. #ifdef DEBUG
  289.     fprintf ( stderr, "Key Press Event\n");
  290. #endif DEBUG
  291.  
  292.     if ( KeyPressInBrowser ( (XKeyEvent *) ev ) == True )
  293.         return;
  294.     stat = (ST *) automate ( ( XKeyEvent *) ev, edwin -> stat );
  295.     if ( (int) stat != -1 )
  296.         edwin -> stat = stat;
  297.  
  298.     if ( IsLastWindow ( 0 ) == True ) {
  299.         XCloseDisplay ( dpy );
  300.         (void) exit (0);
  301.     }
  302. }
  303.  
  304.  
  305. /*
  306. **    Function name : HandleButtonPress
  307. **
  308. **    Description : 
  309. **    Input : 
  310. **    Ouput :
  311. */
  312. static void HandleButtonPress ()
  313. {
  314.     register int     i;
  315.     unsigned int    button;
  316.     int         vm, item;
  317.     Window         w_stat;
  318.     int         result;
  319.  
  320. #ifdef DEBUG
  321.     ( void ) fprintf ( stderr, "ButtonPress %d\n", ev -> xbutton.window );
  322. #endif
  323.  
  324.     if ( ButtonBrowser ( (XButtonEvent *) ev ) == True )
  325.         return;
  326.     /*
  327.      * Button press in menus
  328.      */
  329.     if ( (i = ButtonPressInMenu ( ev -> xbutton.window, edwin -> menu )) != -1 ) {
  330.         w_stat = HandleMenu ( dpy, (XButtonPressedEvent *) ev, edwin->w_frame, edwin->menu, i, &vm, &item );
  331.         ExecMenuFunc ( vm, item );
  332.         if ( w_stat != NULL ) {
  333.             XFindContext ( dpy, w_stat, EdContext,
  334.                 (caddr_t *) &edwin );
  335.         }
  336.         return;
  337.     }
  338.     /*
  339.      * Button press in button window,
  340.      */
  341.     if ( (i = IsButtonInControl ( ev -> xbutton.window )) >= NULL ) {
  342.         if ( GetCursorStat ( edwin -> text ) != OFF )
  343.                 TextCursorOff ( edwin -> text );
  344.         ButtonPressInControl ( ev, edwin -> text, i );
  345.         if ( GetCursorStat ( edwin -> text ) != ON )
  346.             TextCursorOn ( edwin -> text );
  347.         return;
  348.     }
  349.     
  350.     /* 
  351.      * Button press in scroll cursor,
  352.      */
  353.     if ( ButtonPressInScroll ( edwin -> scroll, ev -> xbutton.window, 
  354.         ev -> xbutton.y, &result ) == True) {
  355.         switch ( result ) {
  356.         case CURSOR:
  357.             if ( GetCursorStat ( edwin -> text ) != OFF )
  358.                 TextCursorOff ( edwin -> text );
  359.             if ( GetScrollStat( edwin -> text ))
  360.                 GotoLeft ( edwin -> text );
  361.             if ( (TextInBuf ( edwin -> text ) == True) 
  362.                 && ( GetNbLinesInBuf ( edwin -> text ) > 1))            
  363.                 HandleScrollBar ( dpy, edwin -> scroll, ScrollNLine );
  364.             if ( GetCursorStat ( edwin -> text ) != ON )
  365.                 TextCursorOn ( edwin -> text );
  366.             RefreshScrollBar ( dpy, edwin -> scroll ); 
  367.             break;
  368.         case NEXT:
  369.             TextCursorOff ( edwin -> text );
  370.             NextPage ( edwin -> text  );
  371.             TextCursorOn ( edwin -> text );
  372.             break;
  373.         case PREVIOUS:
  374.             TextCursorOff ( edwin -> text );
  375.             PreviousPage ( edwin -> text );
  376.             TextCursorOn ( edwin -> text );
  377.             break;
  378.         }
  379.         return;
  380.     }
  381.  
  382.     /*
  383.      * Button press in current text window
  384.     * Update cursor position and grap pointer until button release.
  385.     */
  386.     if ( ev -> xbutton.window == edwin -> text -> window ) {
  387.  
  388.            button = ev -> xbutton.button;
  389.  
  390.            XGrabPointer ( dpy, edwin -> w_frame, True,
  391.              ButtonReleaseMask, GrabModeAsync, GrabModeAsync,
  392.              edwin -> w_frame, None, CurrentTime );
  393.  
  394.         switch ( ev -> xbutton.button ) {
  395.         case Button1:
  396.             TextCursorOff ( edwin -> text );
  397.             (void) MoveToXYinTextWindow ( edwin -> text,
  398.                 ev -> xbutton.x, ev -> xbutton.y );
  399.             TextCursorOn ( edwin -> text );
  400.             break;
  401.         case Button2:
  402.             if ( ev -> xbutton.state == ControlMask ) 
  403.                 DeleteBytesFromCutBuffer ( edwin -> text );
  404.             else 
  405.                 GetBytesFromCutBuffer ( edwin -> text );
  406.             break;
  407.         case Button3:
  408.             TextCursorOff ( edwin -> text );
  409.             XSync ( dpy, False );
  410.             StoreBytesInCutBuffer ( edwin -> text,
  411.                 ev -> xbutton.x, ev -> xbutton.y );
  412.             TextCursorOn ( edwin -> text );
  413.             break;
  414.         }
  415.         (void) WaitButtonRelease ( button );
  416.     }
  417. }
  418.  
  419.  
  420. /*
  421. **    Function name : WaitButtonRelease
  422. **
  423. **    Description : 
  424. **    Input : 
  425. **    Ouput :
  426. */
  427. void WaitButtonRelease ( button )
  428.     register unsigned int button;
  429. {
  430.     XEvent event;
  431.  
  432.     for (;;) {
  433.         XNextEvent ( dpy, &event );
  434.         switch ( event.type ) {
  435.         case ButtonRelease:
  436.             if ( event.xbutton.button == button ) {
  437.                 XUngrabPointer ( dpy, CurrentTime );
  438.                 return;
  439.             }
  440.         }
  441.     }
  442. }
  443.  
  444.  
  445. /*
  446. **    Function name : HandleExpose
  447. **
  448. **    Description : 
  449. **    Input : 
  450. **    Ouput :
  451. */
  452. static void HandleExpose ()
  453. {
  454.         static XRectangle rec [2];
  455.     static Region region;
  456.     EdWin         **t;
  457.  
  458. #ifdef DEBUG
  459.     ( void ) fprintf ( stderr, "Expose %d\n", ev -> xexpose.window );
  460. #endif
  461.  
  462.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  463.         if ( *t == NULL ) continue;
  464.         if ( ev -> xexpose.window == ( *t ) -> w_frame 
  465.              || ev -> xexpose.window == GetMenuWindowBar ( (*t) -> menu )  ) {
  466. #ifdef DEBUG
  467.             ( void ) fprintf ( stderr, "Expose Refreshbar\n" );
  468. #endif
  469.             RefreshMenuBar ( dpy, ( *t ) -> menu );
  470.  
  471.             while ( XCheckWindowEvent ( dpy,
  472.                 ev -> xexpose.window,
  473.                 ExposureMask, ev ));
  474.             return;
  475.         }
  476.         
  477.            if ( ev -> xexpose.window == GetTextWindow ( ( *t ) -> text )) {
  478.       
  479. #ifdef DEBUG
  480.             ( void ) fprintf ( stderr, "Expose text window\n" );
  481. #endif
  482.             ExposeTextWindow ( dpy, ( *t ) -> text, ev );
  483.             return;
  484.         }
  485.  
  486.         if ( ExposeInControlePanel ( ev -> xexpose.window, ( *t ) -> mwin ) == True ) {
  487. #ifdef DEBUG
  488.             ( void ) fprintf ( stderr, "Expose control panel\n" );
  489. #endif
  490.             return;
  491.         }
  492.  
  493.         if ( ExposeInScroll ( dpy, ev -> xexpose.window, ( *t ) -> scroll ) == True ) {
  494. #ifdef DEBUG
  495.                  (void) fprintf ( stderr, "Expose sroll\n");
  496. #endif
  497.             return;
  498.         }
  499.     }
  500.     ExposeBrowser ( ev );
  501. }
  502.  
  503.